package com.buss.product.dao.impl;

import java.util.HashMap;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.buss.core.dic.RecordStatus;
import com.buss.core.persistence.BaseDao;
import com.buss.core.persistence.OrderBy;
import com.buss.core.persistence.Paginalbe;
import com.buss.core.persistence.ResultList;
import com.buss.core.utils.StringUtil;
import com.buss.product.dao.CatePropGroupDao;
import com.buss.product.model.CatePropGroup;

@Repository
public class CatePropGroupDaoImpl implements CatePropGroupDao {

	@Autowired
	private BaseDao<CatePropGroup> baseDao;
	
	@Override
	public CatePropGroup savePropGroup(CatePropGroup propGroup) {
		return baseDao.saveOrEdit(propGroup);
	}

	@Override
	public void editPropGroup(CatePropGroup propGroup) {
		baseDao.edit(propGroup);
	}

	@Override
	public void removePropGroup(Long[] ids) {
		String sql = "delete from CatePropGroup propGroup where propGroup.id in (:ids)";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("ids", ids);
		baseDao.excuteHsql(sql, params);
	}

	@Override
	public CatePropGroup loadPropGroup(Long id) {
		return baseDao.get(id, CatePropGroup.class);
	}

	@Override
	public ResultList<CatePropGroup> queryCatePropGroup(String companyId,
			String name, RecordStatus status, OrderBy orderBy, Paginalbe page) {
		String sql = "select propGroup from CatePropGroup propGroup where propGroup.companyId = :companyId ";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("companyId", companyId);
		if(StringUtil.isNotEmpty(name)){
			sql += " and propGroup.name like :name or propGroup.displayName like :name ";
			params.put("name", "%" + name + "%");
		}
		if(status != null){
			sql += " and propGroup.status = :status ";
			params.put("status", status);
		}
		if(orderBy != null){
			orderBy = new OrderBy();
			orderBy.add("sortOrder", OrderBy.DESC);
			sql += orderBy.aggOrder("propGroup");
		}		
		return baseDao.queryHsql(sql, params, page);
	}

	@Override
	public ResultList<CatePropGroup> queryCatePropGroupByCategoryId(
			String companyId, Long categoryId,RecordStatus status, OrderBy orderBy, Paginalbe page) {
		String sql = "select propGroup from GoodsCategory category inner join category.propGroup proGroup " +
				" where category.companyId = :companyId and category.id = :categoryId ";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("companyId", companyId);
		params.put("categoryId", categoryId);
		if(status != null){
			sql += " and propGroup.status = :status ";
			params.put("status", status);
		}
		if(orderBy == null){
			orderBy = new OrderBy();
			orderBy.add("sortOrder", OrderBy.ASC);
		}
		sql += orderBy.aggOrder("propGroup");
		return baseDao.queryHsql(sql, params, page);
	}

	public ResultList<CatePropGroup> queryCatePropGroupByParentCategoryId(String companyId,Long[] categoryIds,RecordStatus status,OrderBy orderBy,Paginalbe page){
		String sql = "select a.id id,a.name name,a.displayName displayName,b.categoryId categoryId " +
				"from base_catepropgroup a,base_category_propgroup b " +
				"where a.id = b.groupId and b.categoryId in (:categoryIds) and a.companyId = :companyId ";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("companyId", companyId);
		params.put("categoryIds", categoryIds);
		if(status != null){
			sql += " and a.status = :status ";
			params.put("status", status.toString());
		}
		if(orderBy == null){
			orderBy = new OrderBy();
			orderBy.add("sortOrder", OrderBy.ASC);
		}
		sql += orderBy.aggOrder("a");
		SQLQuery query = baseDao.getSession().createSQLQuery(sql);
		query.addScalar("id", StandardBasicTypes.LONG)
			.addScalar("name")
			.addScalar("displayName")
			.addScalar("categoryId",StandardBasicTypes.LONG)
			.setResultTransformer(Transformers.aliasToBean(CatePropGroup.class));
		return baseDao.queryNative(sql, query, params, page);
	}
	
	public ResultList<CatePropGroup> queryCatePropGroupNotByParentCategoryId(String companyId,Long[] categoryIds,RecordStatus status,OrderBy orderBy,Paginalbe page){
		String sql = "select id,name,displayName from base_catepropgroup " +
				"where id not in (select groupId from base_category_propgroup " +
				"where categoryId in (:categoryIds)) and companyId = :companyId order by sortOrder";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("companyId", companyId);
		params.put("categoryIds", categoryIds);
		if(status != null){
			sql += " and status = :status ";
			params.put("status", status.toString());
		}
		if(orderBy == null){
			orderBy = new OrderBy();
			orderBy.add("sortOrder", OrderBy.ASC);
		}
		sql += orderBy.aggOrder("");
		SQLQuery query = baseDao.getSession().createSQLQuery(sql);
		query.addScalar("id", StandardBasicTypes.LONG)
			.addScalar("name")
			.addScalar("displayName")
			.setResultTransformer(Transformers.aliasToBean(CatePropGroup.class));
		ResultList<CatePropGroup> rs = baseDao.queryNative(sql, query, params, null);
		return rs;
	}
}
